<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/**
 * Body of the stats moving average stats crons
 * @author Duleepa Wijayawardhana <dups@bioware.com>
 * @version Tue Oct 24 14:49:42 2006
 */

cronstart(CRON_ID, "CRON_NO_DUPLICATE_PROCESS");

//When did I last end?
$sql = "SELECT last_end
    FROM ma_cronstatus
    WHERE ma_type = '".MA_TYPE."'";
$aResult = db_fetch_result(db_query($sql, array("dbHandle" => "bigdaddystats")));

//SetCronLogEntry( CRON_ID, "Step 1 Done");

$nLastEnd = $aResult["last_end"];

if ($nLastEnd < 10000)
{
    $nLastEnd = INSTALL_DATE-86400;
    
}

//Set up the time for now.
$nNow = time();

//Set the wait time out on the original connection as we seem to have some issues
$sql = "SET WAIT_TIMEOUT=28800";
db_query($sql);

$nDoTime = $nLastEnd;

$sql = "SELECT sm.ID, sm.serverID, sm.moduleID
        FROM server_module sm
        LEFT JOIN module m ON m.ID = sm.moduleID
        WHERE m.type = 2";
$aModules = db_fetch_array(db_query($sql));

//SetCronLogEntry( CRON_ID, "Step 2 Done");

//var_dump($aModules);

while ($nDoTime < $nNow - (MA_INCREMENT_MINUTE * 60))
{
    $nMaxDoTime = $nDoTime + (MA_INCREMENT_MINUTE * 60);
    
    if (is_array($aModules))
    {
        foreach ($aModules as $aModule)
        {
            $sql = "DELETE FROM ".MA_TABLE." WHERE
                    serverModuleID = '".$aModule["ID"]."' AND
                    mon_lastreport = '".$nDoTime."'";
            db_query($sql, array("dbHandle" => "bigdaddystats"));
            
            $sql = "SELECT ROUND(AVG(lastreport),3) as average
                FROM server_module_history
                WHERE serverModuleID = ".$aModule["ID"]."
                    AND mon_lastreport BETWEEN $nDoTime AND $nMaxDoTime";
                //echo $sql;
            $aResult = db_fetch_result(db_query($sql, array("dbHandle" => "bigdaddystats")));
            
            if (is_array($aResult) && $aResult["average"] != NULL)
            {
                $sql = "INSERT INTO ".MA_TABLE." SET
                    lastreport = '".$aResult["average"]."',
                    mon_lastreport = '".$nDoTime."',
                    serverID = '".$aModule["serverID"]."',
                    serverModuleID = ".$aModule["ID"].",
                    moduleID = '".$aModule["moduleID"]."'";
                //echo $sql;
                db_query($sql, array("dbHandle" => "bigdaddystats"));
            }
        }
    }
    
     //No set the last end time
    $sql = "UPDATE ma_cronstatus SET
        last_end = $nDoTime
        WHERE ma_type = '".MA_TYPE."'";
    db_query($sql, array("dbHandle" => "bigdaddystats"));
    
    $nDoTime = $nMaxDoTime;
    
    //SetCronLogEntry( CRON_ID, "Done For $nDoTime");
   
}

//SetCronLogEntry( CRON_ID, "Step 3 Done");

cronend(CRON_ID);
?>
